<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html><head><title>Programming in Lua : 26</title>
<link rel="stylesheet" type="text/css" href="pil.css">
</head>
<body>
<p class="warning">
<A HREF="contents.html"><IMG TITLE="Programming in Lua (first edition)" SRC="capa.jpg" ALT="" ALIGN="left"></A>This first edition was written for Lua 5.0. While still largely relevant for later versions, there are some differences.<BR>The third edition targets Lua 5.2 and is available at <A HREF="http://www.amazon.com/exec/obidos/ASIN/859037985X/theprogrammil3-20">Amazon</A> and other bookstores.<BR>By buying the book, you also help to <A HREF="../donations.html">support the Lua project</A>.
</p>
<table width="100%" class="nav">
<tr>
<td width="10%" align="left"><a href="25.3.html"><img border="0" src="left.png" alt="Previous"></a></td>
<td width="80%" align="center"><a href="contents.html"><font face="Helvetica,Arial,sanserif">
<font color="gray">Programming in </font><font color="blue"> Lua</font>
</font></a></td>
<td width="10%" align="right"><a href="26.1.html"><img border="0" src="right.png" alt="Next"></a></td>
</tr>
<tr>
<td width="10%" align="left"></td>
<td width="80%" align="center"><a href="contents.html#P4">Part IV. The C API</a>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href="contents.html#26">Chapter 26. Calling C from Lua</a></td>
<td width="10%" align="right"></td></tr>
</table>
<hr/>
<h1>26 &ndash; Calling C from Lua</h1>

<p>

<p>One of the basic means for extending Lua
is for the application to <em>register</em>
new C functions into Lua.

<p>When we say that Lua can call C functions,
this does not mean that Lua can call any C function.
(There are packages that allow Lua to call any C function,
but they are neither portable nor robust.)
As we saw previously,
when C calls a Lua function,
it must follow a simple protocol to pass the arguments and
to get the results.
Similarly,
for a C function to be called from Lua,
it must follow a protocol
to get its arguments and to return its results.
Moreover, for a C function to be called from Lua,
we must register it, that is,
we must give its address to Lua in an appropriate way.

<p>When Lua calls a C function,
it uses the same kind of stack that C uses to call Lua.
The C function gets its arguments from the stack
and pushes the results on the stack.
To distinguish the results from other values on the stack,
the function returns (in C) the number of results it
is leaving  on the stack.
An important concept here is that the stack is not a global structure;
each function has its own private local stack.
When Lua calls a C function,
the first argument will always be at index 1 of this local stack.
Even when a C function calls Lua code that calls the
same (or another) C function again,
each of these invocations sees only its own private stack,
with its first argument at index 1.

<hr/>
<table width="100%" class="nav">
<tr valign="top">
<td width="90%" align="left">
<small>
  Copyright &copy; 2003&ndash;2004 Roberto Ierusalimschy.  All rights reserved.
</small>
</td>
<td width="10%" align="right"><a href="26.1.html"><img border="0" src="right.png" alt="Next"></a></td>
</tr>
</table>


</body></html>

